package com.neudu.yiyang.controller;

import com.github.pagehelper.PageInfo;
import com.neudu.yiyang.commons.JSONReturn;
import com.neudu.yiyang.pojo.Backdown;
import com.neudu.yiyang.service.BackdownService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class BackdownController {

    @Autowired
    private BackdownService backdownService;
    @Autowired
    private JSONReturn jsonReturn;

//    //获取所有退住信息（backdown表的）
//    @RequestMapping("get_all_backdowns")
//    public String getAll(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
//                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {
//        try {
//            PageInfo<Backdown> pageInfoReturn = backdownService.getAllBackdownsWithCustomerName(pageNum, pageSize);
//            return jsonReturn.returnSuccess(pageInfoReturn);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return jsonReturn.returnError(e.getMessage());
//        }
//    }
//
//    //根据关键字获取退住信息
//    //根据已退住客户姓名进行模糊查询
//    @RequestMapping("get_backdowns_by_keyword")
//    public String getByKeyword(@RequestParam("pageNum") int pageNum,
//                               @RequestParam("pageSize") int pageSize,
//                               @RequestParam(required = false) String keyword) {
//        try {
//            PageInfo<Backdown> pageInfoReturn = backdownService.getBackdownsByKeywordWithCustomerName(keyword, pageNum, pageSize);
//            Map<String, Object> result = new java.util.HashMap<>();
//            result.put("pageNum", pageInfoReturn.getPageNum());
//            result.put("pageSize", pageInfoReturn.getPageSize());
//            result.put("total", pageInfoReturn.getTotal());
//            result.put("list", pageInfoReturn.getList());
//            return jsonReturn.returnSuccess(result);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return jsonReturn.returnError(e.getMessage());
//        }
//    }

    //添加一条退住信息
    @RequestMapping("add_one_backdown")
    public String addOne(@RequestBody Backdown backdown) {
        try {
            backdownService.addBackdown(backdown);
            // 判断退住类型，如果是正常退住或死亡退住，更新房间状态为空闲
            //新增退住信息，isRetreated为1
            if (backdown.getRetreattype() == 0 || backdown.getRetreattype() == 1) {
                backdownService.updateBedStatus(backdown.getCustomerId(), 1); // 1 表示空闲
            }
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_backdown_by_id")
    public String getBackdownByCustomerId(@RequestParam("customerId") Integer customerId) {
        try {
            Backdown backdown = backdownService.getBackdownByCustomerId(customerId);
            if (backdown != null) {
                return jsonReturn.returnSuccess(backdown);
            } else {
                return jsonReturn.returnError("未找到对应的退住记录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
    //根据退住信息 ID 修改退住信息
    @RequestMapping("modify_backdown_by_id")
    public String modifyBackdownByCustomerId(@RequestBody Backdown backdown) {
        try {
            backdownService.updateBackdownByCustomerId(backdown);
            return jsonReturn.returnSuccess(backdown);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

}